#include "MonitoringChangePass.h"
#include "ui_MonitoringChangePass.h"
#include <QDebug>
MonitoringChangePass::MonitoringChangePass(QWidget *parent,userModel*model) :
    QWidget(parent),
    ui(new Ui::MonitoringChangePass),
    model(model),
    userSqlite(MonitoringUserSqlite::getInstance())
{
    ui->setupUi(this);
    connect(ui->pushButton,&QPushButton::clicked,this,[=](){
        LOG_INFO("点击了修改密码");
        qDebug()<<ui->lineEdit->text()<<ui->lineEdit_2->text()<<ui->lineEdit_3->text();
        if(ui->lineEdit->text()==ui->lineEdit_2->text()){
             //emit changeClick("新旧密码一样，无需修改");
            ui->label->setText("新旧密码一样，无需修改");
            return ;
        }
        if(ui->lineEdit_2->text()!=ui->lineEdit_3->text()){
             ui->label->setText("两次输入的密码不一致");
            return ;
        }
//       if(userSqlite.loginGetData(model->username,ui->lineEdit->text())!=QUERY_SUCCESS){
//            emit changeClick("旧密码错误");
//           return ;
//       }
         LOG_INFO("用户"+model->id+"修改了密码");
       if (SnaillTcpClient::instance()->sendJsonData(MonitoringTool::getUserJson("changepass",model->username,ui->lineEdit_2->text(),"",""))) {
                  //  qDebug() << "Login data sent successfully";
              } else {
                  ui->label->setText("登录失败，请检查网络");
                  return ;
              }
       connect(SnaillTcpClient::instance(), &SnaillTcpClient::dataReceived, this, &MonitoringChangePass::handleDataReceived);


    });
}
void MonitoringChangePass::handleDataReceived(const QByteArray &data)
{
    qDebug()<<"==+++=="<<data;
    QJsonDocument jsonDoc = QJsonDocument::fromJson(data);
    QJsonObject jsonObj = jsonDoc.object();
    QString action = jsonObj["action"].toString();
    if(action=="changepass"){
                model->password=ui->lineEdit_2->text();
        //        if(userSqlite.changePassword(model->username,model->password)==QUERY_SUCCESS){
        //            emit changeClick("修改成功");
        //        }else{
        //            emit changeClick("修改失败");
        //        }
        QString coded = jsonObj["code"].toString();
        int code = coded.toInt();
        if(code==QUERY_SUCCESS){
             ui->label->setText("修改成功");

        }else {
              ui->label->setText("修改失败");
        }

    }


}
MonitoringChangePass::~MonitoringChangePass()
{
    delete ui;
}
